using System;
using System.Linq;
using JNPF.Utils;
using System.Threading.Tasks;
using JNPF.Data;
using JNPF.Data.Util;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using JNPF.Basics.Models;

namespace JNPF.Basics.Logic
{
    public class RenewableresourcesService : DataFactory, RenewableresourcesIService
    {
        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        public async Task<RenewableresourcesEntity> GetInfo(int id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<RenewableresourcesEntity>(m => m.Id == id);
            }
        }

        /// <summary>
        /// 列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<RenewableresourcesEntity>> GetPageList(RenewableresourcesListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<RenewableresourcesEntity>();
                var queryParam = input.queryJson.ToObject();
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }
                //查询资源内容
                if (!string.IsNullOrEmpty(input.remark))
                {
                    queryWhere = queryWhere.And(m => m.Remark.Contains(input.remark));
                }
                //查询重量
                if (input.weight.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Weight == input.weight);
                }
                //查询金额
                if (input.amount.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Amount == input.amount);
                }



                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<RenewableresourcesEntity>().Where(queryWhere).GetPageResultAsync(input);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Create(RenewableresourcesEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(RenewableresourcesEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(RenewableresourcesEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<List<RenewableresourcesEntity>> GetList(RenewableresourcesListQueryInput input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<RenewableresourcesEntity>();
                //查询客户主键
                if (input.customerId.HasValue)
                {
                    queryWhere = queryWhere.And(m => m.CustomerId == input.customerId);
                }
                //查询资源内容
                if (!string.IsNullOrEmpty(input.remark))
                {
                    queryWhere = queryWhere.And(m => m.Remark.Contains(input.remark));
                }
                //查询重量
                if (input.weight.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Weight == input.weight);
                }
                //查询金额
                if (input.amount.ToInt() != 0)
                {
                    queryWhere = queryWhere.And(m => m.Amount == input.amount);
                }



                queryWhere = queryWhere.And(m => m.DeleteMark == 0);
                return await db.GetIQueryable<RenewableresourcesEntity>().Where(queryWhere).ToListAsync();
            }
        }


    }
}